﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NewsCrawler.Common.Interfaces;
using NewsCrawler.Common.Models.ViewModels;
using Raven.Client.Document;
using NewsCrawler.Common.Models;

namespace DataAccess
{
    public class RavenRepository : IRepository
    {
        List<Article> IRepository.GetNewsByCategoryId(string categoryId)
        {
            return Articles.FindAll((a) => a.Category.CategoryId == categoryId);
        }

        IndexViewModel IRepository.GetIndexViewModel()
        {
            IndexViewModel model;
            //using (var documentStore = new DocumentStore() { Url = "http://localhost:8082" })
            //{
            //    documentStore.Initialize();
            //    using (var documentSession = documentStore.OpenSession())
            //    {
            model = new IndexViewModel
                {
                    Categories = Categories,
                    FlashNews = GetFlashNews()
                };
            //    }
            //}

            return model;
        }

        private List<Article> GetFlashNews()
        {
            var results = from article in Articles
                          orderby article.PublishDate descending
                          select article;
            return results.Take(3).ToList();
        }

        static readonly List<Category> Categories = new List<Category>
                                    {new Category("Category1"), new Category("Category2"), new Category("Category3")};

        private static readonly List<Article> Articles = new List<Article>
                                                             {
                                                                 new Article
                                                                     {
                                                                         Title = "haber1",
                                                                         Category = Categories[0],
                                                                         PublishDate = DateTime.Now.AddDays(-1)
                                                                     },
                                                                 new Article
                                                                     {
                                                                         Title = "haber2",
                                                                         Category = Categories[0],
                                                                         PublishDate = DateTime.Now.AddDays(-1)
                                                                     },
                                                                 new Article
                                                                     {
                                                                         Title = "haber3",
                                                                         Category = Categories[1],
                                                                         PublishDate = DateTime.Now.AddDays(-2)
                                                                     },
                                                                 new Article
                                                                     {
                                                                         Title = "haber4",
                                                                         Category = Categories[1],
                                                                         PublishDate = DateTime.Now.AddDays(-3)
                                                                     },
                                                                 new Article
                                                                     {
                                                                         Title = "haber5",
                                                                         Category = Categories[1],
                                                                         PublishDate = DateTime.Now.AddDays(-4)
                                                                     },
                                                                 new Article
                                                                     {
                                                                         Title = "haber6",
                                                                         Category = Categories[1],
                                                                         PublishDate = DateTime.Now.AddDays(-5)
                                                                     },
                                                                 new Article
                                                                     {
                                                                         Title = "haber7",
                                                                         Category = Categories[2],
                                                                         PublishDate = DateTime.Now.AddDays(-6)
                                                                     }
                                                             };

        Category IRepository.GetCategoryByName(string name)
        {
            return Categories.Find((c) => c.Name == name);
        }
    }
}
